<?php

class Logger_File extends Logger
{

	/**
	 * @var string
	 */
	protected $_file = null;

	/**
	 * @var array
	 * log data store and write once
	 */
	protected $_data = array();

	/**
	 * constructor
	 * @param string $name
	 * @param array $config
	 */
	protected function __construct($name, $config)
	{

		/**
		 * call parent constructor
		 */
		parent::__construct($name, $config);

		/**
		 * check correct file name
		 */
		$this -> _file = $this -> getConfig('filename', TEMP_PATH . '/log/main.log');

		/**
		 * do not add regisiter shutdown function, it sometime may touch
		 */
	}

	/**
	 * write message to file
	 * @param     string/int  $level
	 * @param     string      $message
	 */
	public function write($level, $message)
	{
		$this -> _data[] = $this -> format($level, $message);
		return $this;
	}

	public function flush()
	{

		if ($this->_data && $fp = fopen($this -> _file, 'a+'))
		{
			fwrite($fp, implode(PHP_EOL, $this -> _data));
			fclose($fp);
		}
		$this -> _data = array();
	}

}
